#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Prepare pre-refinement                            		    #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 29/07/2016                                             #
# Last Modification: 29/07/2016                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 4
#
#
# MANUAL: This script prepares the first refinement in single-particle mode with FREALIGN, based on the crystal averages calculated in the previous step.
#
# DISPLAY: SPR_DIR
# DISPLAY: SPR_WhichStack
# DISPLAY: SPR_PREREFINE_DIR
# DISPLAY: SPR_FRL_cluster_type
# DISPLAY: SPR_FRL_nprocessor_ref
# DISPLAY: SPR_FRL_nprocessor_rec
# DISPLAY: SPR_FRL_mem_per_cpu
# DISPLAY: SPR_FRL_start_process
# DISPLAY: SPR_FRL_end_process
# DISPLAY: SPR_FRL_res_high_refinement
# DISPLAY: SPR_FRL_res_high_class
# DISPLAY: SPR_FRL_thresh_reconst
# DISPLAY: SPR_FRL_outer_radius
# DISPLAY: SPR_FRL_inner_radius
# DISPLAY: SPR_FRL_mol_mass
# DISPLAY: SPR_SYM
# DISPLAY: sample_pixel
# DISPLAY: stepdigitizer
# DISPLAY: CS
# DISPLAY: KV
# DISPLAY: SPR_FRL_XSTD
# DISPLAY: SPR_FRL_PBC
# DISPLAY: SPR_FRL_parameter_mask
# DISPLAY: SPR_FRL_refineangleinc
# DISPLAY: SPR_FRL_refineshiftinc
# DISPLAY: SPR_FRL_SIG_CHG_ANGLES
# DISPLAY: SPR_FRL_SIG_CHG_SHIFTS
# DISPLAY: SPR_FRL_res_reconstruction
# DISPLAY: SPR_FRL_res_low_refinement
# DISPLAY: SPR_FRL_FFILT
# DISPLAY: SPR_FRL_FMATCH
# DISPLAY: SPR_FRL_FBEAUT
# DISPLAY: SPR_FRL_FBOOST
# DISPLAY: SPR_FRL_mp_cpus
# DISPLAY: SPR_FRL_restart_after_crash
# DISPLAY: SPR_FRL_delete_scratch
# DISPLAY: SPR_FRL_mask_file
# DISPLAY: SPR_FRL_image_contrast
# DISPLAY: SPR_FRL_thresh_fsc_ref
# DISPLAY: SPR_FRL_thresh_fsc_eval
# DISPLAY: SPR_FRL_res_min
# DISPLAY: SPR_FRL_ref_stay_away
# DISPLAY: SPR_FRL_change_pmask
# DISPLAY: SPR_FRL_fix_theta
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""

set scriptname = PrepPreRefineFREALIGN

#
set SPR_DIR = ""
set SPR_WhichStack = ""
set SPR_PREREFINE_DIR = ""
set SPR_FRL_cluster_type = ""
set SPR_FRL_nprocessor_ref = ""
set SPR_FRL_nprocessor_rec = ""
set SPR_FRL_mem_per_cpu = ""
set SPR_FRL_start_process = ""
set SPR_FRL_end_process = ""
set SPR_FRL_res_high_refinement = ""
set SPR_FRL_res_high_class = ""
set SPR_FRL_thresh_reconst = ""
set SPR_FRL_outer_radius = ""
set SPR_FRL_inner_radius = ""
set SPR_FRL_mol_mass = ""
set SPR_SYM = ""
set sample_pixel = ""
set stepdigitizer = ""
set CS = ""
set KV = ""
set SPR_FRL_XSTD = ""
set SPR_FRL_PBC = ""
set SPR_FRL_parameter_mask = ""
set SPR_FRL_refineangleinc = ""
set SPR_FRL_refineshiftinc = ""
set SPR_FRL_res_reconstruction = ""
set SPR_FRL_res_low_refinement = ""
set SPR_FRL_FFILT = ""
set SPR_FRL_FMATCH = ""
set SPR_FRL_FBEAUT = ""
set SPR_FRL_FBOOST = ""
set SPR_FRL_mp_cpus = ""
set SPR_FRL_restart_after_crash = ""
set SPR_FRL_delete_scratch = ""
set SPR_FRL_mask_file = ""
set SPR_FRL_image_contrast = ""
set SPR_FRL_SIG_CHG_ANGLES = ""
set SPR_FRL_SIG_CHG_SHIFTS = ""
set SPR_FRL_thresh_fsc_ref = ""
set SPR_FRL_thresh_fsc_eval = ""
set SPR_FRL_res_min = ""
set SPR_FRL_ref_stay_away = ""
set SPR_FRL_change_pmask = ""
set SPR_FRL_fix_theta = ""

#
# FREALIGN needs amplitude contrast instead of phase contrast, but here we don't need CTF correction so set it to -1.0.
# set SPR_ampcon = `echo "${phacon}" | awk '{printf "%.4f \n", sqrt(1-$1*$1)}'`
set SPR_ampcon = -1.0
# For the pre-refinement the number of classes will always be 1:
set SPR_FRL_nclasses = "1"
#
set SPR_STACKS_DIR = ${SPR_DIR}/stacks/
set SPR_STACK_ROOTNAME = 'particles'
set SPR_FREALIGN_DIR = ${SPR_DIR}/FREALIGN/
set SPR_PREREFINE_DIR = ${SPR_FREALIGN_DIR}/${SPR_PREREFINE_DIR}/
set SPR_PREREFINE_MPARAMETERS = ${SPR_PREREFINE_DIR}/mparameters
#$end_vars

if ( -e LOGS/${scriptname}.results ) then
	mv LOGS/${scriptname}.results LOGS/${scriptname}.results.old
endif

# Start script commands:

set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#

# Check if initial files exist:
if ( ! -e ${SPR_PREREFINE_DIR} ) then
	mkdir -p ${SPR_PREREFINE_DIR}
endif
if ( ! -e ${SPR_PREREFINE_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStack}_crystal-avg_1_r1.par ) then
	# ln -s ${SPR_STACKS_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStack}_crystal-avg_1_r1.par ${SPR_PREREFINE_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStack}_crystal-avg_1_r1.par
	cp ${SPR_STACKS_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStack}_crystal-avg_1_r1.par ${SPR_PREREFINE_DIR}
endif

if ( ! -e ${SPR_PREREFINE_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStack}_crystal-avg.mrc ) then
	ln -s ${SPR_STACKS_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStack}_crystal-avg.mrcs ${SPR_PREREFINE_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStack}_crystal-avg.mrc
endif

if ( -e ${SPR_PREREFINE_MPARAMETERS} ) then
	mv ${SPR_PREREFINE_MPARAMETERS} ${SPR_PREREFINE_MPARAMETERS}.old
endif

if ( ${SCRATCH_DISK} == "." ) then
	set SCRATCH_DISK = ""
endif

#################################################################################
# GENERATE THE MPARAMETERS FILE FOR FREALIGN
#################################################################################

echo "Control parameter file to run Frealign" >> ${SPR_PREREFINE_MPARAMETERS}
echo "======================================" >> ${SPR_PREREFINE_MPARAMETERS}
echo "" >> ${SPR_PREREFINE_MPARAMETERS}
echo "This file must me kept in the project working directory from which the refinement scripts are launched." >> ${SPR_PREREFINE_MPARAMETERS}
echo "" >> ${SPR_PREREFINE_MPARAMETERS}
echo "Note: Please make sure that project and scratch directories (if specified) are accessible by all sub-processes that are run on cluster nodes." >> ${SPR_PREREFINE_MPARAMETERS}
echo "" >> ${SPR_PREREFINE_MPARAMETERS}
echo "# Computer-specific setting" >> ${SPR_PREREFINE_MPARAMETERS}
echo "cluster_type         ${SPR_FRL_cluster_type}		! Set to "sge", "lsf", "slurm", "pbs" or "condor" when running on a cluster, otherwise set to "none"." >> ${SPR_PREREFINE_MPARAMETERS}
echo "nprocessor_ref       ${SPR_FRL_nprocessor_ref}		! Number of CPUs to use during refinement." >> ${SPR_PREREFINE_MPARAMETERS}
echo "nprocessor_rec       ${SPR_FRL_nprocessor_rec}		! Number of CPUs to use during reconstruction." >> ${SPR_PREREFINE_MPARAMETERS}
echo "mem_per_cpu	     ${SPR_FRL_mem_per_cpu}	! Memory available per CPU (in MB)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "" >> ${SPR_PREREFINE_MPARAMETERS}
echo "# Refinement-specific parameters" >> ${SPR_PREREFINE_MPARAMETERS}
echo "MODE                  1		! 1, 2, 3, 4 or 5. Refinement mode, normally 1. Set to 2 for additional search." >> ${SPR_PREREFINE_MPARAMETERS}
echo "start_process         ${SPR_FRL_start_process}         ! First cycle to execute. Output files from previous cycle (n-1) required." >> ${SPR_PREREFINE_MPARAMETERS}
echo "end_process           ${SPR_FRL_end_process}  	! Last cycle to execute." >> ${SPR_PREREFINE_MPARAMETERS}
echo "res_high_refinement   ${SPR_FRL_res_high_refinement}	! High-resolution limit for particle alignment." >> ${SPR_PREREFINE_MPARAMETERS}
echo "res_high_class        ${SPR_FRL_res_high_class}        ! High-resolution limit to calculate class membership (OCC)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "nclasses              ${SPR_FRL_nclasses}		! Number of classes to use." >> ${SPR_PREREFINE_MPARAMETERS}
echo "DANG                5.0	! Mode 3, 4 and 5: Angular step for orientational search." >> ${SPR_PREREFINE_MPARAMETERS}
echo "ITMAX               50		! Mode 2 and 4: Number of repetitions of grid search with random starting angles." >> ${SPR_PREREFINE_MPARAMETERS}
echo "" >> ${SPR_PREREFINE_MPARAMETERS}
echo "# Dataset-specific parameters" >> ${SPR_PREREFINE_MPARAMETERS}
echo "data_input           ${SPR_STACK_ROOTNAME}_${SPR_WhichStack}_crystal-avg	! Root name for parameter and map files." >> ${SPR_PREREFINE_MPARAMETERS}
echo "raw_images           ${SPR_STACK_ROOTNAME}_${SPR_WhichStack}_crystal-avg.mrc" >> ${SPR_PREREFINE_MPARAMETERS}
echo "image_contrast        ${SPR_FRL_image_contrast}		! N or P. Set to N if particles are dark on bright background, otherwise set to P." >> ${SPR_PREREFINE_MPARAMETERS}
echo "outer_radius        ${SPR_FRL_outer_radius} 	! Outer radius of spherical particle mask in Angstrom." >> ${SPR_PREREFINE_MPARAMETERS}
echo "inner_radius          ${SPR_FRL_inner_radius}	! Inner radius of spherical particle mask in Angstrom." >> ${SPR_PREREFINE_MPARAMETERS}
echo "mol_mass            ${SPR_FRL_mol_mass}	! Molecular mass in kDa of particle or helical segment." >> ${SPR_PREREFINE_MPARAMETERS}
echo "Symmetry              ${SPR_SYM}         ! Symmetry of particle." >> ${SPR_PREREFINE_MPARAMETERS}
echo "pix_size              ${sample_pixel}	! Pixel size of particle in Angstrom." >> ${SPR_PREREFINE_MPARAMETERS}
echo "dstep                 ${stepdigitizer}	! Pixel size of detector in micrometer." >> ${SPR_PREREFINE_MPARAMETERS}
echo "Aberration            ${CS}	! Sherical aberration coefficient in millimeter." >> ${SPR_PREREFINE_MPARAMETERS}
echo "Voltage             ${KV}	! Beam accelleration voltage in kilovolt." >> ${SPR_PREREFINE_MPARAMETERS}
echo "Amp_contrast        ${SPR_ampcon}	! Amplitude contrast." >> ${SPR_PREREFINE_MPARAMETERS}
echo "" >> ${SPR_PREREFINE_MPARAMETERS}
echo "# Expert parameters (for expert users)" >> ${SPR_PREREFINE_MPARAMETERS}
echo "XSTD                  ${SPR_FRL_XSTD}	! Tighter masking of 3D map (XSTD > 0) or particles (XSTD < 0)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "PBC                   ${SPR_FRL_PBC}	! Discriminate particles with different scores during reconstruction. Small values (5 - 10) discriminate more than large values (50 - 100)." >> ${SPR_PREREFINE_MPARAMETERS}
echo parameter_mask   \"${SPR_FRL_parameter_mask}\" ! Five flags of 0 or 1 \(e.g. 1 1 1 1 1\). Determines which parameters are refined \(PSI, THETA, PHI, SHX, SHY\). >> ${SPR_PREREFINE_MPARAMETERS}
echo "refineangleinc        ${SPR_FRL_refineangleinc} 	! When larger than 1: Alternate between refinement of OCC and OCC + angles." >> ${SPR_PREREFINE_MPARAMETERS}
echo "refineshiftinc        ${SPR_FRL_refineshiftinc} 	! When larger than 1: Alternate between refinement of OCC and OCC + angles + shifts." >> ${SPR_PREREFINE_MPARAMETERS}
echo "sigma_angles        ${SPR_FRL_SIG_CHG_ANGLES}		! When greater than 0: Restrains the Euler angles to avoid they change too much in one cycle (STD in degrees)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "sigma_shifts        ${SPR_FRL_SIG_CHG_SHIFTS} 	! When greater than 0: Restrains the x,y shifts to avoid they change too much in one cycle (STD in Angstrom)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "res_reconstruction    ${SPR_FRL_res_reconstruction}	! High-resolution limit of reconstruction. Normally set to Nyquist limit." >> ${SPR_PREREFINE_MPARAMETERS}
echo "res_low_refinement    ${SPR_FRL_res_low_refinement}	! Low-resolution limit for particle alignment. Set to particle dimention or larger." >> ${SPR_PREREFINE_MPARAMETERS}
echo "thresh_reconst        ${SPR_FRL_thresh_reconst}       ! Particles with scores below this value will not be included in the reconstruction." >> ${SPR_PREREFINE_MPARAMETERS}
echo "thresh_refine        50.0	! Mode 4: Score threshold above which search will not be performed." >> ${SPR_PREREFINE_MPARAMETERS}
echo "nbootstrap         1000		! Number of bootstrap volumes to calculate real-space variance map." >> ${SPR_PREREFINE_MPARAMETERS}
echo "FMAG                  F		! T or F. Set to T to refine particle magnification. Not recommended in most cases." >> ${SPR_PREREFINE_MPARAMETERS}
echo "FDEF                  F		! T or F. Set to T to refine defocus per micrograph. Not recommended in most cases." >> ${SPR_PREREFINE_MPARAMETERS}
echo "FASTIG                F		! T or F. Set to T to refine astigmatism. Not recommended in most cases." >> ${SPR_PREREFINE_MPARAMETERS}
echo "FPART                 F		! T or F. Set to T to refine defocus for each particle. Not recommended in most cases." >> ${SPR_PREREFINE_MPARAMETERS}
echo "FFILT                 ${SPR_FRL_FFILT}		! T or F. Set to T to apply optimal filter to reconstruction. Recommended in most cases." >> ${SPR_PREREFINE_MPARAMETERS}
echo "FMATCH                ${SPR_FRL_FMATCH}		! T or F. Set to T to output matching projections. Only needed for diagnostics." >> ${SPR_PREREFINE_MPARAMETERS}
echo "FBEAUT                ${SPR_FRL_FBEAUT}		! T or F. Set to T to apply symmetry also in real space. Not needed in most cases." >> ${SPR_PREREFINE_MPARAMETERS}
echo "FBOOST                ${SPR_FRL_FBOOST}		! T or F. Set to T to allow potential overfitting during refinement. Not recommended in most cases." >> ${SPR_PREREFINE_MPARAMETERS}
echo "RBfactor              0.0	! B-factor sharpening (when < 0) applied during refinement. Not recommended in most cases." >> ${SPR_PREREFINE_MPARAMETERS}
echo "beam_tilt_x           0.0	! Beam tilt in mrad along X-axis." >> ${SPR_PREREFINE_MPARAMETERS}
echo "beam_tilt_y           0.0	! Beam tilt in mrad along y-axis." >> ${SPR_PREREFINE_MPARAMETERS}
echo "mp_cpus               ${SPR_FRL_mp_cpus}		! Number of CPUs to use for each reconstruction job." >> ${SPR_PREREFINE_MPARAMETERS}
echo "restart_after_crash   ${SPR_FRL_restart_after_crash}		! T or F. Set to T to restart job if it crashes." >> ${SPR_PREREFINE_MPARAMETERS}
echo "delete_scratch        ${SPR_FRL_delete_scratch}		! Delete intermediate files in scratch directory." >> ${SPR_PREREFINE_MPARAMETERS}
echo "qsub_string_ref  ""		! String to add to cluster jobs submitted for refinement (only for SGE and PBS clusters)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "qsub_string_rec  ""		! String to add to cluster jobs submitted for reconstruction (only for SGE and PBS clusters)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "first_particle        " >> ${SPR_PREREFINE_MPARAMETERS}
echo "last_particle         " >> ${SPR_PREREFINE_MPARAMETERS}
echo "frealign_bin_dir      " >> ${SPR_PREREFINE_MPARAMETERS}
echo "scratch_dir           ${SCRATCH_DISK}" >> ${SPR_PREREFINE_MPARAMETERS}
echo "" >> ${SPR_PREREFINE_MPARAMETERS}
echo "# Masking parameters (for expert users)" >> ${SPR_PREREFINE_MPARAMETERS}
echo "mask_file             ${SPR_FRL_mask_file}" >> ${SPR_PREREFINE_MPARAMETERS}
echo "mask_edge             0		! Width of cosine edge in pixels to add around mask. Set to 0 to leave mask unchanged." >> ${SPR_PREREFINE_MPARAMETERS}
echo "mask_outside_weight   0.0	! Factor to downweight density outside of mask (normally 0.0 - 1.0)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "mask_filt_res         0.0	! Filter radius (in A) to low-pass filter density outside density. Set to 0.0 to skip filtering." >> ${SPR_PREREFINE_MPARAMETERS}
echo "mask_filt_edge        0		! Width of cosine edge in reciprocal pixels to add to filter function." >> ${SPR_PREREFINE_MPARAMETERS}
echo "" >> ${SPR_PREREFINE_MPARAMETERS}
echo "focus_mask       ""             ! Four numbers (in Angstroms) describing a spherical mask (X, Y, Z for mask center and R for mask radius)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "" >> ${SPR_PREREFINE_MPARAMETERS}
echo "# Auto-refinement parameters (only used if calling frealign_run_refine_auto script)" >> ${SPR_PREREFINE_MPARAMETERS}
echo "thresh_fsc_ref          ${SPR_FRL_thresh_fsc_ref}              ! Auto-refiner will take the resolution where the FSC crosses this threshold as limit for the refinement (typically 0.4 to 0.8)." >> ${SPR_PREREFINE_MPARAMETERS}
echo "thresh_fsc_eval         ${SPR_FRL_thresh_fsc_eval}    ! Auto-refiner will evaluate map improvement looking at the resolution where the FSC crosses this threshold." >> ${SPR_PREREFINE_MPARAMETERS}
echo "res_min                 ${SPR_FRL_res_min}     ! Auto-refiner won't ever use a resolution lower than this as the limit for the refinement." >> ${SPR_PREREFINE_MPARAMETERS}
echo "ref_stay_away           ${SPR_FRL_ref_stay_away}              ! Auto-refiner won't ever use a limit for alignment that is less than this value away from the current map's resolution." >> ${SPR_PREREFINE_MPARAMETERS}
echo "change_pmask            ${SPR_FRL_change_pmask}               ! T or F. Set to T to allow auto-refiner to try different combinations of parameter_mask if necessary." >> ${SPR_PREREFINE_MPARAMETERS}
echo "no_theta			${SPR_FRL_fix_theta} 		! T or F. Set to T to keep the tilt angle THETA fixed (unchanged) in auto-refinement. Useful for 2D crystal data." >> ${SPR_PREREFINE_MPARAMETERS}


echo ":: "
echo ":: New mparameters file has been generated. Old one has been backed-up."
echo ":: "
#################################################################################
# END OF MPARAMETERS FILE GENERATION
#################################################################################
